package org.shangma.interceptor;

import io.seata.core.context.RootContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * 定义拦截器，传递事务ID
 */
public class XIDInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        if(!StringUtils.isEmpty(RootContext.getXID())){
            request.getHeaders().add(RootContext.KEY_XID,RootContext.getXID());
        }
        return execution.execute(request,body);
    }
}
